<?php
/* This file is part of Mirasol CMS
   (C) 2011 by Alban Technologies. Written by Chris Alban Hansen.
   Released under the terms of the GNU General Public License.
   See COPYING in the top level directory of the Mirasol CMS installation. */

include "{$_SERVER['DOCUMENT_ROOT']}/includes/config.php";
include "{$_SERVER['DOCUMENT_ROOT']}/includes/xmlfeed.php";

if ($loadsubpage == 1)
{
  $tid = isset ($_GET['tid']) ? $_GET['tid'] : 0;
  
  /* Get site name */
  $result = mysql_query ("SELECT sitename FROM ".db_maketablename ($table_settings)." LIMIT 1");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      $sitename = htmlentities ($row['sitename'], ENT_COMPAT, "UTF-8", false);
    }
  mysql_free_result ($result);
?>
<h1><?=$sitename?></h1>

<div id="templates">
  <h3>Templates</h3>
  <table cellpadding="0" cellspacing="0" style="width: 100%">
<?php
/* Print the list of templates */
$result = mysql_query ("SELECT id, name FROM ".db_maketablename ($table_templates)." ORDER BY name");
if (mysql_num_rows ($result) > 0)
  {
    while ($row = mysql_fetch_array ($result))
      {
        $name = htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false).".xsl";
        print "<tr><td><a href=\"./?p=templates&amp;tid={$row['id']}\"><strong>$name</strong></a></td>";
        print "<td class=\"alignright\"><a href=\"javascript:deltemplate('$name', {$row['id']});\" title=\"Delete '$name'\"><strong>&times;</strong></a></td></tr>\n";
      }
  }
mysql_free_result ($result);
?>
  </table>

  <div id="addnew1">
    <p>
      <input type="button" value="+" onclick="document.getElementById('addnew2').style.display='block';" class="button" />
      Add new template
    </p>
  </div>

  <div id="addnew2">
    <form action="../includes/addtemplate.php" method="post">
      <table cellpadding="0" cellspacing="0">
        <tr>
          <td><strong>Name:</strong>&nbsp;</td>
          <td><input type="text" name="name" value="Type_name_here" maxlength="50" class="textfield" />.xsl</td>
        </tr>
        <tr>
          <td colspan="2"><input type="submit" value="Add &raquo;" class="button" /></td>
        </tr>
      </table>
    </form>
  </div>
</div>

<div id="editor">
<?php
/* If no template is selected... */
if ($tid == 0)
{
?>
  <div id="info">
    <p>&larr; Click on an item in the list to edit it.</p>
  </div>
<?php
}
else
{
?>
    <div id="tabs">
      <ul>
        <li><span>Template</span></li>
      </ul>
    </div>
    
    <form action="../includes/savetemplate.php" method="post">
      <div id="tab1">
<?php
  $result = mysql_query ("SELECT name, fields, xslt FROM ".db_maketablename ($table_templates)." WHERE id='$tid'");
  if (mysql_num_rows ($result) > 0)
    {
      $row = mysql_fetch_array ($result);
      
      $layout = htmlentities ($row['xslt'], ENT_COMPAT, "UTF-8", false);
      
      /* Parse fields and xmlfeed */
      print "<label>Name</label><br /><input type=\"text\" name=\"templatename\" value=\"".htmlentities ($row['name'], ENT_COMPAT, "UTF-8", false)."\" class=\"textfield\" />\n";
      
      /* Prepare the XML parser and parse the data */
      $xmldata = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n".$row['fields'];     
      $xmlres = xml_parser_create ("utf-8");
      xml_parse_into_struct ($xmlres, $xmldata, $values);
      xml_parser_free ($xmlres);
?>
      <table cellpadding="0" cellspacing="0">
        <thead>
          <td class="col1"><label>Field name</label></td>
          <td class="col2"><label>Label</label></td>
          <td class="col3"><label>Type</label></td>
          <td class="col3">&nbsp;</td>
        </thead>
<?php
      
      /* Walk through the tags */
      $i = 0;
      $fieldcount = 0;
      while ($values[$i])
        {
          if ($values[$i]['level'] == 2 && strcasecmp ($values[$i]['tag'], "field") == 0 && isset ($values[$i]['attributes']['NAME']) && isset ($values[$i]['attributes']['LABEL']) && isset ($values[$i]['attributes']['TYPE']))
            {
              $fieldcount++;
              print "<tr>";
              print "<td class=\"col1\"><input type=\"text\" name=\"name$fieldcount\" value=\"{$values[$i]['attributes']['NAME']}\" class=\"smalltextfield\" /></td>";
              print "<td class=\"col2\"><input type=\"text\" name=\"label$fieldcount\" value=\"{$values[$i]['attributes']['LABEL']}\" class=\"smalltextfield\" /></td>";
              print "<td class=\"col3\"><select name=\"type$fieldcount\">";
              foreach ($fieldtypes as $type)
                {
                  $sel = $values[$i]['attributes']['TYPE'] == $type ? " selected=\"selected\"" : "";
                  print "<option value=\"$type\"$sel>$type</option>";
                }
              print "</select></td>";
              print "<td><input type=\"submit\" name=\"delfield$fieldcount\" value=\"-\" class=\"alignright\" /></td>";
              print "</tr>\n";
            }
          $i++;
        }
?>
      </table>
      
      <p>
        <input type="submit" name="addfield" value="+" />
        <span class="addfield">Add new field</span>
      </p>
      
      <label>Layout</label>
      <textarea id="xslt" name="templatelayout"><?=$layout?></textarea>
      <script type="text/javascript">
        //<![CDATA[
          var editor = CodeMirror.fromTextArea (document.getElementById ("xslt"), { mode: { name: "xmlpure" } });
        //]]>
      </script>
<?php
    }
  mysql_free_result ($result);
?>
      </div>
      <p class="alignright">&nbsp;<br /><input type="hidden" name="tid" value="<?=$_GET['tid']?>" /><input type="hidden" name="fieldcount" value="<?=$fieldcount?>" /><input type="submit" value="Save" /></p>
    </form>
<?php
}
?>
</div>

<div class="clear"></div>
<?php
}
?>
